package com.vn.ld.btc.dao.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * The persistent class for the orders database table.
 */
@Entity
@Table(name = "orders")
@NamedQuery(name = "Order.findAll", query = "SELECT o FROM Order o")
public class Order implements Serializable, Comparable<Order> {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "order_id", unique = true, nullable = false)
	private String orderId;

	@Column(name = "asset_type", nullable = false)
	private Integer assetType;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "input_date")
	private Date inputDate;

	@Column(name = "original_volume", precision = 10, scale = 6)
	private BigDecimal originalVolume;

	@Column(name = "remain_volume", precision = 10, scale = 6)
	private BigDecimal remainVolume;

	@Column(name = "status", nullable = false)
	private Integer status;

	@Column(nullable = false)
	private Integer type;

	@Column(name = "unit_price", precision = 10, scale = 6)
	private BigDecimal unitPrice;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "update_date")
	private Date updateDate;
	
	@Transient
	private Date customerOrderDate;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "customer_id", nullable = false)
	private Customer customer;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "customer_service_id", nullable = false)
	private CustomerService customerService;

	public Order() {
	}

	public String getOrderId() {
		return this.orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public Integer getAssetType() {
		return this.assetType;
	}

	public void setAssetType(Integer assetType) {
		this.assetType = assetType;
	}

	public Date getInputDate() {
		return this.inputDate;
	}

	public void setInputDate(Date inputDate) {
		this.inputDate = inputDate;
	}

	public BigDecimal getOriginalVolume() {
		return this.originalVolume;
	}

	public void setOriginalVolume(BigDecimal originalVolume) {
		this.originalVolume = originalVolume;
	}

	public BigDecimal getRemainVolume() {
		return this.remainVolume;
	}

	public void setRemainVolume(BigDecimal remainVolume) {
		this.remainVolume = remainVolume;
	}

	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public BigDecimal getUnitPrice() {
		return this.unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public Date getUpdateDate() {
		return this.updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public Customer getCustomer() {
		return this.customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public CustomerService getCustomerService() {
		return this.customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	public Date getCustomerOrderDate() {
		return customerOrderDate;
	}

	public void setCustomerOrderDate(Date customerOrderDate) {
		this.customerOrderDate = customerOrderDate;
	}

	@PrePersist
	private void prePersist() {
		this.inputDate = new Date();
		this.updateDate = new Date();
	}
	
	@PreUpdate
	private void preUpdate() {
		this.updateDate = new Date();
	}

	@Override
	public int compareTo(Order o) {
		
		//compare prices
		if(this.unitPrice.compareTo(o.unitPrice) > 0)  
			return 1;
		else if (this.unitPrice.compareTo(o.unitPrice) <0)
			return -1;
		
		//compare by user order date
		else
		{
			if(this.customerOrderDate.before(o.customerOrderDate))
				return 1;
			else if (this.customerOrderDate.after(o.customerOrderDate))
				return -1;
			else 
				return 0;
		}
			
	}
}
